<?php


namespace app\controller\admin;

use app\common\service\face\FaceDevice;
use app\model\AdminSchoolDevice;
use app\util\ReturnCode;
use think\Response;

class SchoolDevice extends Base
{
    public function index() : Response {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $school_id = $this->request->get('school_id', $this->userInfo['school_id']);
        $serial_type = $this->request->get('serial_type', '');
        $sortKey = $this->request->get('sortKey', 'heart_time');
        $order = $this->request->get('order', 'DESC');

        $where = [];
        $obj = (new AdminSchoolDevice());
        if ($school_id > 0) {
            array_push($where, ['school_id', '=', $school_id]); // 查询报错
        }
        if ($serial_type) {
            array_push($where, ['serial_type', '=', $serial_type]);
        }
        $listObj = $obj->where($where)->with(['school'])
            ->append(['serial_type_text'])
            ->order(['serial_type' => 'DESC', 'school_id' => 'DESC', 'create_time' => 'DESC'])
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        return $this->buildSuccess([
            'list'  => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }

    /**
     * 远程开门
     * @return Response
     */
    public function openDoor(): Response {
        $serial = $this->request->post('serial');
        $operator = $this->request->post('operator');
        $keepOpen = $this->request->post('keepOpen');
        if ($serial) {
            FaceDevice::openDoor($serial, $operator, $keepOpen);
            return $this->buildSuccess();
        }
        return $this->buildFailed(ReturnCode::NOT_EXISTS, '设备不存在');
    }
}